Skip to content

feat(hooks): draft/proposal: support multiple worktrees#180

Merged
zimbatm merged 1 commit intonumtide:masterfrom
tomeon:git-hooks-dir-via-environment
Apr 11, 2022
Merged

feat(hooks): draft/proposal: support multiple worktrees#180
zimbatm merged 1 commit intonumtide:masterfrom
tomeon:git-hooks-dir-via-environment

Conversation

@tomeon
Copy link
Copy Markdown
Contributor

@tomeon tomeon commented Mar 12, 2022

First, thanks for this convenient solution for managing git hooks. It was very easy to get started rolling custom hooks (after cribbing some plays from digga, to be sure 😃 ).

I use git worktree extensively, and it would be nice if devshell supported per-worktree hook definitions (if for no other reason than to more easily validate changes to git hooks made in a feature branch worktree).

This pull request is a proposal to support per-worktree hooks via the use of shim scripts that can safely be installed to the shared hooks directory. These shims read the real hook path from the DEVSHELL_GIT_HOOKS_DIR environment variable, whichs points to a store path containing the actual hook scripts for the current devshell.
The shim scripts cause git hooks to be ignored:

  1. Outside of the devshell (DEVSHELL_DIR is unset or empty), or
  2. When the current devshell doesn't define any git hooks (DEVSHELL_GIT_HOOKS_DIR is unset or empty), or
  3. When the current devshell doesn't define the specific git hook in question (${DEVSHELL_GIT_HOOKS_DIR}/bin/<hook-name> is not executable).

In such cases, the hook shim scripts emit a diagnostic message and exit.

This way, devshell can safely install a shim script to the git hooks directory even if the corresponding hook isn't active for all worktrees, or if different worktrees provide different definitions for the hook, and punt to DEVSHELL_GIT_HOOKS_DIR to activate only those hooks that are pertinent for the current devshell.

Ancillary changes:

  1. Respect GIT_COMMON_DIR on git clients that support it (GIT_COMMON_DIR may affect where git looks for hooks), and
  2. Respect the user's core.hooksPath setting.

Thanks in advance for your consideration.

@tomeon tomeon force-pushed the git-hooks-dir-via-environment branch from 823aecd to 5b06c9e Compare March 12, 2022 21:08
by setting up git hook shim scripts that exec into the corresponding
real hook scripts under ${DEVSHELL_GIT_HOOKS_DIR}/bin if and only
if:

    1. DEVSHELL_DIR is defined,
    2. DEVSHELL_GIT_HOOKS_DIR is defined, and
    3. ${DEVSHELL_GIT_HOOKS_DIR}/bin/<hook-name> is executable.

Otherwise, the hook shim scripts emit a diagnostic message and exit.

This way, we can safely install a hook shim to the git hooks directory
*even if the hook isn't active for all worktrees*, and punt to
DEVSHELL_GIT_HOOKS_DIR to activate only those hooks that are pertinent
for a given devshell.

Ancillary changes:

    1. Respect GIT_COMMON_DIR on git clients that support it
       (GIT_COMMON_DIR may affect where git looks for hooks), and
    2. Respect the user's core.hooksPath setting.
@tomeon tomeon force-pushed the git-hooks-dir-via-environment branch from 5b06c9e to 4a1cead Compare April 10, 2022 21:11
Copy link
Copy Markdown
Member

@zimbatm zimbatm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thank you

@zimbatm zimbatm merged commit 6ec7953 into numtide:master Apr 11, 2022
@tomeon tomeon deleted the git-hooks-dir-via-environment branch April 11, 2022 17:30
tennox pushed a commit to tennox/devshell that referenced this pull request May 17, 2022
by setting up git hook shim scripts that exec into the corresponding
real hook scripts under ${DEVSHELL_GIT_HOOKS_DIR}/bin if and only
if:

    1. DEVSHELL_DIR is defined,
    2. DEVSHELL_GIT_HOOKS_DIR is defined, and
    3. ${DEVSHELL_GIT_HOOKS_DIR}/bin/<hook-name> is executable.

Otherwise, the hook shim scripts emit a diagnostic message and exit.

This way, we can safely install a hook shim to the git hooks directory
*even if the hook isn't active for all worktrees*, and punt to
DEVSHELL_GIT_HOOKS_DIR to activate only those hooks that are pertinent
for a given devshell.

Ancillary changes:

    1. Respect GIT_COMMON_DIR on git clients that support it
       (GIT_COMMON_DIR may affect where git looks for hooks), and
    2. Respect the user's core.hooksPath setting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants